import VueRouter from "vue-router";
import ElementUI from 'element-ui'
import store from "@/store";

export const router = new VueRouter({

    mode:'history',
    routes:[
        {
            path:'/',
            redirect:'/home'
        },
        {
            path:'/index',
            name:'index',
            component:()=>import("@/components/home/Home"),
            meta: { title: '当前位置',},
            children:[
                {
                    path:'/home',
                    name:'home',
                    component:()=>import("@/views/IndexPage"),
                    meta: { title: '首页',},
                },
                {
                    path:'/bookWrite',
                    name:'bookWrite',
                    component:()=>import("@/components/bookWrite/BookWrite"),
                    meta: { title: '书写台',},
                    children:[
                        {
                            path:'write',
                            name:'write',
                            component:()=>import("@/views/WritePage"),
                            meta: { title: '发布文章'},
                        },
                        {
                            path:'article',
                            name:'article',
                            component:()=>import("@/views/MyArticlePage"),
                            meta: { title: '我的文章'}
                        },

                    ]
                },
                {
                    path:'/user',
                    component:()=>import("@/views/UserCore"),
                    meta: { title: '个人中心' }
                },
                {
                    path:'/author',
                    component:()=>import("@/views/AboutAuthor"),
                    meta: { title: '关于作者' }
                },
            ]
        },
        {
            path:"/login",
            component:()=>import("@/components/login/ToLogin")
        }
    ]
})

//路由守卫
let intercept = ['/bookWrite/write','/bookWrite/article','/user'];//拦截地址
router.beforeEach((to,from,next)=>{
    //判断去往的页面是否需要登录
    if ( !(intercept.indexOf(to.fullPath) === -1)){
        //判断是否登录成功
        if(store.getters["LoginModule/getStatus"]){
            //登录成攻放行
            next();
        }else{
            ElementUI.Notification({
                title:"友情提示!",
                message:"登录过期或未登录,请重新登录!",
                type:"warning",
                offset:50
            })
        }
    }else{
        next()
    }

})